from google.appengine.ext import db
from google.appengine.api import users

TYPE_GA = 'ga'
TYPE_FB = 'fb'

class YTUser(db.Model):
    name = db.StringProperty(multiline=False)
    first_name = db.StringProperty(multiline=False)
    last_name = db.StringProperty(multiline=False)
    description = db.TextProperty()
    date_joined = db.DateTimeProperty(auto_now_add=True)
    valid_from = db.DateTimeProperty()
    valid_to = db.DateTimeProperty()
    
    is_superuser = db.BooleanProperty(default=False)
    type = db.StringProperty(multiline=False)
    ga = db.UserProperty(auto_current_user_add=True)

    @classmethod
    def get_current_user(cls):
        ga_user = users.get_current_user()
        if not ga_user:
            return None
        
        key = TYPE_GA + ga_user.user_id()
        user = YTUser.get_by_key_name(key)
          
        if not user:
            user = YTUser.get_or_insert(key_name=key, type=TYPE_GA, ga=ga_user)
            
        return user        
    
    def get_name(self):
        return self.ga.nickname()
